from splider.common import CSVUtil, HttpRequest
import os
import csv


url = 'https://car.autohome.com.cn/'

# 品牌列表
def get_brand():
    current_dir = os.path.abspath('.')
    print(current_dir)
    file_name = os.path.join(current_dir, "data/brand.csv")
    print(file_name)
    with open(file_name, 'wt', newline='')  as csvfile1:
        #header = ['brand','count', 'url']
        writer = csv.writer(csvfile1)
        #writer.writerow(header)
        #访问页面
        response = HttpRequest.fetch_url(url)
        if response:
            print("URL=", url)
            # 处理response
        else:
            print("Failed to fetch the page.")

        brands = response.html.find('.cartree ul li h3 a')
        for brand in brands:
            #print(brand.text)
            #格式化
            bk = brand.text
            start = bk.find("(")
            end = bk.find(")")
            brand_name = bk[0:start]
            Num = bk[(start+1):end]
            brand_url = url + brand.attrs.get("href", None)
            print(brand_url)
            CSVUtil.save2csv(writer,brand_name,Num,brand_url)
    csvfile1.close()
#

if __name__ == '__main__':
    print("开始处理")
    get_brand()
    print("处理结束")